﻿using AutoMapper;
using MediatR;
using Management.API.Application.Controller.Management;
using Management.Domain;
using Management.Errorcode;
using Management.Errorcode.ErrorCode;
using Management.Infrastructure.Interfaces;
using Management.Infrastructure.Interfaces.BOMManagement;
using Management.Models.BOMBasicInformation;
using Management.API.Application.Controller.Management.Material;
using Management.Infrastructure.Repositories.BOMManagement;
using Management.API.DTOs.Management;
using Management.API.Utils;



namespace Management.API.Application.CommandHandler.Management
{
    public class GetMaterialCommandHandler:IRequestHandler<GetMaterialCommand,ApiResult<IList<GetMaterialDTO>>>
    {
        private readonly IMaterialRepository iMaterialRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetMaterialCommandHandler> _logger;

        public GetMaterialCommandHandler(IMaterialRepository iMaterialRepository, IMapper mapper, ILogger<GetMaterialCommandHandler> logger)
        {
            iMaterialRepository = iMaterialRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult<IList<GetMaterialDTO>>> Handle(GetMaterialCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var useList = await iMaterialRepository.GetAllAsync();
                var useLists = _mapper.Map<IList<GetMaterialDTO>>(useList);
                return ApiResult<IList<GetMaterialDTO>>.Success(useLists, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetMaterialCommandHandler");
                throw;
            }

        }


    }
}
